package cn.edu.scau.cmi.xianjiehua.comprehension.dao;

import cn.edu.scau.cmi.xianjiehua.comprehension.domain.Student;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class StudentDao extends BaseHibernateDAO {
    private static final Logger log = LoggerFactory.getLogger(StudentDao.class);
    // property constants
    public static final String NAME = "name";


    public void save(Student transientInstance) {
        log.debug("saving Student instance");
        
        try {
        	Session session = getSession();
        	session.beginTransaction();
            session.save(transientInstance);
            session.getTransaction().commit();
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    public void delete(Student persistentInstance) {
        log.debug("deleting Student instance");
        Session session = getSession();
        try {
            session.getTransaction().begin();
            session.delete(persistentInstance);
            session.getTransaction().commit();
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public Student findById(Long id) {
        log.debug("getting Student instance with id: " + id);
        Session session = getSession();
        try {
            Student instance = session.get(Student.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    @SuppressWarnings("unchecked")
	public List<Student> findByExample(Student instance) {
        log.debug("finding Student instance by example");
        Session session = getSession();
        try {
            List<Student> results = session.createCriteria(Student.class)
                    .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    @SuppressWarnings("unchecked")
	public List<Student> findByProperty(String propertyName, Object value) {
        log.debug("finding Student instance with property: " + propertyName + ", value: " + value);
        Session session = getSession();
        try {
            String queryString = "from Student as model where model." + propertyName + "= ?";
            Query queryObject = session.createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<Student> findByName(Object name) {
        return findByProperty(NAME, name);
    }

    @SuppressWarnings("unchecked")
	public List<Student> findAll() {
        log.debug("finding all Student instances");
        Session session = getSession();
        try {
            String queryString = "from Student";
            Query queryObject = session.createQuery(queryString);
            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public Student merge(Student detachedInstance) {
    	Session session = getSession();
        log.debug("merging Student instance");
        try {
        	session.getTransaction().begin();
            Student result = (Student) session.merge(detachedInstance);
            session.getTransaction().commit();
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Student instance) {
        log.debug("attaching dirty Student instance");
        Session session = getSession();
        try {
        	session.getTransaction().begin();
        	session.saveOrUpdate(instance);
            session.getTransaction().commit();
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void attachClean(Student instance) {
        log.debug("attaching clean Student instance");
        Session session = getSession();
        try {
        	session.getTransaction().begin();
        	session.buildLockRequest(LockOptions.NONE).lock(instance);
            session.getTransaction().commit();
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

}
